package com.canbot.u05.activity.reception;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.bean.UsherDataInfo;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.HashMap;

/**
 * 迎宾模式设置界面
 */
public class ReceptionModeSettingActivity extends ReceptionBaseActivity implements View.OnClickListener {

        private ImageView ivWelcomText, ivWelcomeSpeech;

        private ImageView btnBack;

        private UsherDataInfo usherDataInfo;

        private ToastTools toastTools;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_reception_mode_setting);
                toastTools = ToastTools.newInstance(this);
                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.USHER_DIR_PATH + "ybszyd_ybms_u05_001.wav");
                setViews();
                initData();
        }
        private Dialog dialogLoading;
        private void initData() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.loading));
                String url = IpMsgConst.HTTP_URL_CHEST + "usherModeAction";
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("type", 8);
                OkHttpUtils.upLoadFile(url, hashMap, new OkHttpUtils.OkHttpCallBack() {

                        @Override
                        public void onSuccess(String result) {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                usherDataInfo = JSON.parseObject(result, UsherDataInfo.class);
                        }

                        @Override
                        public void onFailure() {
                                LoadingDialogUtils.closeDialog(dialogLoading);
                                toastTools.showText(getString(R.string.network_error));
                                finish();
                        }
                });
        }


        private void setViews() {
                btnBack = (ImageView) findViewById(R.id.usher_mode_setting_imageView_back);
                ivWelcomText = (ImageView) findViewById(R.id.iv_usherMode_setting_text);
                ivWelcomeSpeech = (ImageView) findViewById(R.id.iv_usherMode_setting_speech);
                ivWelcomText.setOnClickListener(this);
                ivWelcomeSpeech.setOnClickListener(this);
                btnBack.setOnClickListener(this);
                findViewById(R.id.iv_usherMode_setting_bg).setOnClickListener(this);

        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.usher_mode_setting_imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_usherMode_setting_text:
                                //点击欢迎文字按钮
                                Intent intent = new Intent(this, ReceptionModeSettingTextActivity.class);
                                if (usherDataInfo != null) {
                                        String tts = usherDataInfo.getTts();
                                        if (!TextUtils.isEmpty(tts)) {
                                                intent.putExtra("tts", tts);
                                        }
                                }
                                startActivityForResult(intent, 100);
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_usherMode_setting_speech:
                                //点击欢迎语音按钮
                                intent = new Intent(this, ReceptionModeSettingSpeechActivity.class);
                                if (usherDataInfo != null) {
                                        int voiceType = usherDataInfo.getVoiceType();
                                        intent.putExtra("voiceType", voiceType);
                                        String voiceContent = usherDataInfo.getVoiceContent();
                                        if (TextUtils.isEmpty(voiceContent)) {
                                                intent.putExtra("voiceContent", voiceContent);
                                        }
                                }
                                startActivityForResult(intent, 101);
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_usherMode_setting_bg:
                                //设置欢迎背景图
                                intent = new Intent(this, ReceptionModeChoosePicActivity.class);
                                if (usherDataInfo != null) {
                                        String picPath = usherDataInfo.getPicPath();
                                        if (!TextUtils.isEmpty(picPath)) {
                                                intent.putExtra("picPath", picPath);
                                        }
                                }

                                startActivityForResult(intent, 102);
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if (resultCode == 100 && resultCode == RESULT_OK) {
                        String tts = data.getStringExtra("tts");
                        usherDataInfo.setTts(tts);
                }
                if (resultCode == 101 && resultCode == RESULT_OK) {
                        int voiceType = data.getIntExtra("voiceType",-1);
                        String voiceContent = data.getStringExtra("voiceContent");
                        usherDataInfo.setVoiceType(voiceType);
                        usherDataInfo.setVoiceContent(voiceContent);
                }

                if (resultCode == 102 && resultCode == RESULT_OK) {
                        String picPath = data.getStringExtra("picPath");
                        usherDataInfo.setPicPath(picPath);
                }
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
        }
}
